package com.nf.health.app.activity.hostopal.adapter;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.activity.home.MainHomeActivity;
import com.nf.health.app.activity.hostopal.model.MyHopstipalListBaen;
import com.nf.health.app.activity.miansearch.adapter.DefaultSelectCallback;
import com.nf.health.app.customview.CircleImageView;
import com.nf.health.app.glide.GlideUtil;
import com.nf.health.app.utils.ActivityUtils;

import java.util.List;
import java.util.concurrent.locks.ReadWriteLock;

/**
 * Created by Administrator on 2017/4/18/018.
 */

public class MyHopitalListAdapter extends BaseAdapter {
    private List<MyHopstipalListBaen> list;
    private Context cnt;
    private LayoutInflater layoutInflater;
    private int nowPosition = -1;// 当前点击了的条目
    private DefaultSelectCallback selectCallback;
    public MyHopitalListAdapter(Context cnt, List<MyHopstipalListBaen> list){
        this.cnt = cnt;
        this.list = list;
        layoutInflater = LayoutInflater.from(cnt);
    }

    public void setMyNotifyDataChanged (List<MyHopstipalListBaen> list){
        this.list = list;
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return  list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if(convertView == null){
            convertView = layoutInflater.inflate(R.layout.item_myhopitallist,null);
            viewHolder = new ViewHolder();
            viewHolder.iv_hostipal_head = (CircleImageView) convertView.findViewById(R.id.iv_hostipal_head);
            viewHolder.tv_hostipal_name = (TextView) convertView.findViewById(R.id.tv_hostipal_name);
            viewHolder.tv_item_type_hostipal_jg = (TextView) convertView.findViewById(R.id.tv_item_type_hostipal_jg);
            viewHolder.tv_persion_hostipal = (TextView) convertView.findViewById(R.id.tv_persion_hostipal);
            viewHolder.tv_user_hostipal_num = (TextView) convertView.findViewById(R.id.tv_user_hostipal_num);
            viewHolder.rl_marchen = (RelativeLayout) convertView.findViewById(R.id.rl_marchen);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
         final MyHopstipalListBaen myHopstipalListBaen = list.get(position);
        GlideUtil.loadNetImg(cnt,viewHolder.iv_hostipal_head,myHopstipalListBaen.getHimg(),R.drawable.default_hospital);
        String type = myHopstipalListBaen.getLevel()+"";
        if(TextUtils.isEmpty(type)){
            viewHolder.tv_item_type_hostipal_jg.setVisibility(View.GONE);
        }else{
            viewHolder.tv_item_type_hostipal_jg.setVisibility(View.VISIBLE);
            viewHolder.tv_item_type_hostipal_jg.setText(type+"");
        }

        String name = myHopstipalListBaen.getHname()+"";
        if(TextUtils.isEmpty(name)){
            viewHolder.tv_hostipal_name.setVisibility(View.GONE);

        }else{
            viewHolder.tv_hostipal_name.setVisibility(View.VISIBLE);
            viewHolder.tv_hostipal_name.setText(name+"");
        }
         int  dNum =myHopstipalListBaen.getDnum();
         int  sNum =myHopstipalListBaen.getSnum();

        String resultdoc = "";
        if(dNum != 0){
            if(sNum == 0){
                resultdoc = "医生: 共"+dNum+"名 ";
            }else{
                resultdoc = "医生: 共"+dNum+"名 "+" 专家：共"+sNum+"名";
            }
            viewHolder.tv_persion_hostipal.setText(resultdoc+"");
        }else{
            if(sNum  != 0){
                resultdoc =  " 专家：共"+sNum+"名";
                viewHolder.tv_persion_hostipal.setText(resultdoc+"");
            }else{
                viewHolder.tv_persion_hostipal.setVisibility(View.GONE);
            }
        }




        //关注用户数
        String usum = myHopstipalListBaen.getUnum()+"";
        if(TextUtils.isEmpty(usum)){
            viewHolder.tv_user_hostipal_num.setVisibility(View.GONE);

        }else{
            viewHolder.tv_user_hostipal_num.setVisibility(View.VISIBLE);
            viewHolder.tv_user_hostipal_num.setText("关注用户："+usum+"");
        }

        viewHolder.rl_marchen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString(MainHomeActivity.DOCTOR_DETAIL_HID,myHopstipalListBaen.getHid()+"");
                ActivityUtils.showActivity(cnt,MainHomeActivity.class,bundle);
            }
        });
        return convertView;
    }
    public void setMyChicked(int index,TextView textView){
        textView.setEnabled(true);
    }

    public void setDoctorCommentData(List<MyHopstipalListBaen> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    private class ViewHolder{
        private TextView tv_hostipal_name, tv_item_type_hostipal_jg,tv_persion_hostipal,tv_user_hostipal_num;
        private CircleImageView iv_hostipal_head;
        private RelativeLayout rl_marchen;
    }

}
